/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_handlers
 * FILE       : ArithGebraProcessHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Feb 14, 2011  mycodetalks			       Created
 */
package net.handler;

import java.util.ArrayList;
import java.util.List;

import net.entity.AttemptedQuestion;
import net.entity.Data;
import net.entity.QuestionInfo;

/**
 * Class description goes here
 */
public class ArithmaticProcessHandler extends ProcessHandler implements IProcessHandler {
	
	//-----------------------------------------------------------------------------------
	// Constructors
	//-----------------------------------------------------------------------------------
	public ArithmaticProcessHandler(String operation) {
		super(operation);
	}

	//-----------------------------------------------------------------------------------
	// Abstract methods
	//-----------------------------------------------------------------------------------
	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(true);
	}
	
	private QuestionInfo generateSingle(boolean htmlquestion) {
		QuestionInfo qinfo = new QuestionInfo();
		Data data = new Data();
		qinfo.setQuestionObject(data);
		qinfo.setAnswer(data.getAnswer());
		qinfo.setKey(getOperation());
		if(htmlquestion) {
			qinfo.setHtmlQuestion(this.generateHtmlQuestion(data));
		}
		return qinfo;
	}
	
	@Override
	public List<QuestionInfo> generateMultiple() {
		List<QuestionInfo> questions = new ArrayList<QuestionInfo>();
		for(int i=0; i<10; i++) {
			QuestionInfo question = generateSingle(false);
			question.setQuestionNumber(i+1);
			questions.add(question);
		}
		return questions;
	}
	
	private String generateHtmlQuestion(Data data) {
		StringBuffer sbHtml = new StringBuffer("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" >");
		sbHtml.append("<tr>");
			sbHtml.append("<td class=\"data\" >" + String.valueOf(data.getData1()) + "</td>");
			sbHtml.append("<td> &nbsp; </td>");
			sbHtml.append("<td class=\"data\" > + </td>");
			sbHtml.append("<td> &nbsp; </td>");
			sbHtml.append("<td class=\"data\" >" + String.valueOf(data.getData2()) + "</td>");
			sbHtml.append("<td class=\"data\" > = </td>");
			sbHtml.append("<td class=\"question\" > ? </td>");
		sbHtml.append("</tr>");
		sbHtml.append("</table>");
		
		return sbHtml.toString();
		
	}

	
	public boolean matchAnswer(String useranswer, AttemptedQuestion attemptedQuestion) {
		//remove "," if any
		if(useranswer != null && useranswer.trim().length() != 0 && useranswer.equals(attemptedQuestion.getAnswer())) {
			return true;
		}

		return false;
	}
	

}



